<?php

/**
 * index.php 主页面
 * 
 * @author 老洪
 * @link www.baidu.com
 * @since 2020年12月9日15:14:00
 * @copyright CGX
 */
header("Content-Type:text/html;charset=utf-8");

//引入连接数据库文件 require
require './lib/init.php';
//查询所有的文章
// $sql = "select * from art";
// $data = mGetAll($sql);

//判断用户是否登录
// $_SESSION['s_username'] = null;
session_start();
$sessions = empty($_SESSION['s_username']) ? '' : $_SESSION['s_username'];
if ($sessions == '') {
  //用户名或密码不存在，弹出‘该用户不存在’
  echo "<script>alert('请先登录账户！');window.location.href='login.php'</script>";
  exit();
}

// var_dump($pages);
if (isset($_GET['cat_id'])) {
  $where = 'and cat.cat_id=' . $_GET['cat_id'];
  //$res = mGetRow('select num from cat where cat_id=' . $_GET['cat_id']);
  $res = mGetRow('select count(*) as num from art left join cat on art.cat_id = cat.cat_id where art.cat_id=' . $_GET['cat_id']);
  /**
   * var_dump($res);
   * 
   * E:\wampserver\www\blog\index.php:24:
   *  array (size=1)
   *   'num' => string '5' (length=1)
   */
  $num = $res['num'];
} else {
  $where = '';
  $sql = "select count(*) as num from art";
  $num = mGetOne($sql); //获取总页数
}
$cnt = 3; //每页显示三篇文章
$curr = isset($_GET['page']) ? $_GET['page'] : 1; //当前页码数，从地址栏的page值获取
$pages = cPager($num, $cnt, $curr);
$sql = 'select art.*,cat.catname from art left join cat on art.cat_id = cat.cat_id where 1 ' . $where . ' order by art.art_id desc limit ' . ($curr - 1) * $cnt . ',' . $cnt;
// $sql = "select * from art";
$data = mGetAll($sql);

$category = "select * from cat";
$dataC = mGetAll($category);
// var_dump($data);

require './view/front/index.html';
